function CustomerManager(template){
    var ap = new AjaxOperator();
    this.customers = null;
    this.template = template;
    
    this.findAll=function(){
        ap.sendGet("findAll",function(data){
        	this.customers = data;
        	_render(this.customers);
        });
    }
    
    this.addCustomer=function(customer){
        ap.sendPost("addCustomer",customer,function(data){
        	if(data){
        		this.customers.push(data);
            	alert("add success");
            	_render(this.customers);
        	}else {
        		alert("add fail");
        	}
        });
    }
    
    this.updateCustomer=function(customer){
    	ap.sendPut("updateCustomer",customer,function(data){
    		if(data){
    			var cindex=-1;
        		this.customers.forEach(function(item,index){
        			if(item.id==data.id){
        				cindex=index;
        			}
        		});
        		this.customers.splice(cindex,1,data);
        		alert("update success");
        		_render(this.customers);
    		}else {
    			alert("update fail");
    		}
    		$("#updateModal").modal('hide');
    	});
    }
    
    function _render(data){
    	template.empty();
        if(!customers.length){
            return;
        }
        data.forEach((item,index)=>{
            var tr = $("<tr>");
            tr.append($("<td>").text(item.id));
            tr.append($("<td>").text(item.name));
            if(item.sex){
                tr.append($("<td>").text("male"));
            }else {
                tr.append($("<td>").text("female"));
            }
            tr.append($("<td>").text(item.salary));
            tr.append($("<td>").text(_formatDate(new Date(item.birth))));
            tr.append($("<td>").append(
                $("<button>").addClass("btn btn-primary delete").attr("id",item.id).text("delete").data(item))
                .append("&nbsp;&nbsp;")
                .append($("<button>").addClass("btn btn-primary update")
                		.attr("id",item.id)
                		.attr("data-toggle","modal")
                		.attr("data-target","#updateModal").text("update").data(item)));
            template.append(tr);
        });
        _bindDeleteEvent();
        _bindUpdateEvent();
    }
    
    //添加删除事件
    function _bindDeleteEvent(){
    	$("button.delete").on("click",function(e){
            ap.sendDelete("deleteCustomer?cid="+$(this).data().id,function(data){
            	if(data){
            		alert("delete success")
            		this.customers = this.customers.filter(function(item){
                		return item.id!=data.id;
                	});
            		_render(this.customers);
            	}else {
            		alert("delete fail");
            	}
            });
        });
    }
    
    //添加更新事件，点击进入更新页面并填充数据
    function _bindUpdateEvent(){
    	$("button.update").on("click",function(e){
    		var data = $(this).data();
    		$("#updateModal input[name='id']").val(data.id);
        	$("#updateModal input[name='name']").val(data.name);
        	$("#updateModal input[name='salary']").val(data.salary);
            if(data.sex){
                $("#updateModal input[name='sex']").last().attr("checked",false);
                $("#updateModal input[name='sex']").first().attr("checked",true);
            }else {
                $("#updateModal input[name='sex']").first().attr("checked",false);
                $("#updateModal input[name='sex']").last().attr("checked",true);
            }
        	$("#updateModal input[name='birth']").val(_formatDate(new Date(data.birth)));
        });
    }
    
    //格式化时间
    function _formatDate(date){
    	var year = date.getFullYear();
	    var month = date.getMonth() + 1;
	    var strDate = date.getDate();
	    if (month >= 1 && month <= 9) {
	        month = "0" + month;
	    }
	    if (strDate >= 0 && strDate <= 9) {
	        strDate = "0" + strDate;
	    }
	    return  year + "-" + month + "-" + strDate;
    }
}